package org.xm.sk.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xm.sk.exception.BaseException;
import org.xm.sk.vo.ob.ResultMsg;

@RestController
@ControllerAdvice
public class ErrorHandler {

    private static final Logger logger = LoggerFactory
            .getLogger(ErrorHandler.class);
    
    @ExceptionHandler(value = BaseException.class)
    public @ResponseBody ResultMsg baseErrorHandler(HttpServletRequest req,
            HttpServletResponse rsp, Exception e) {
        return dataMap(req, rsp, e, -1);
    }

    @ExceptionHandler(value = InternalAuthenticationServiceException.class)
    public @ResponseBody ResultMsg inAuthErrorHandler(HttpServletRequest req,
            HttpServletResponse rsp, Exception e) {
        return dataMap(req, rsp, e, 500);
    }

    @ExceptionHandler(value = BadCredentialsException.class)
    public @ResponseBody ResultMsg badCredErrorHandler(HttpServletRequest req,
            HttpServletResponse rsp, Exception e) {
        return dataMap(req, rsp, e, 500);
    }

    @ExceptionHandler(value = AccessDeniedException.class)
    public @ResponseBody ResultMsg accessDeniedErrorHandler(
            HttpServletRequest req, HttpServletResponse rsp, Exception e) {
        return dataMap(req, rsp, e, 403);
    }

    @ExceptionHandler(value = Exception.class)
    public @ResponseBody ResultMsg defaultErrorHandler(HttpServletRequest req,
            HttpServletResponse rsp, Exception e) {
        return dataMap(req, rsp, e, -1);
    }

    private ResultMsg dataMap(HttpServletRequest req, HttpServletResponse rsp,
            Exception e, int reCode) {
        if (logger.isErrorEnabled()) {
            logger.error(
                    "---DefaultException Handler---Host {} invokes url {} ERROR: {}",
                    req.getRemoteHost(), req.getRequestURL(), e.getMessage());
        }
        e.printStackTrace();
        ResultMsg errorMsg = new ResultMsg();
        String msg = e.getMessage();
        if (0 < reCode) {
            errorMsg.setReCode(reCode + "");
            rsp.setStatus(reCode);
        } else {
            if ("No handler found".equals(msg)) {
                errorMsg.setReCode("404");
                rsp.setStatus(404);
            } else {
                errorMsg.setReCode("500");
                rsp.setStatus(500);
            }
        }
        errorMsg.setReMsg(msg);
        errorMsg.setTime(new Date());
        return errorMsg;
    }

}
